﻿using System;
using System.Collections.Generic;
using System.Linq;
using Framework.Exceptions.RepositoryExceptions;
using Interfaces.Repositories;
using Interfaces.RepositoryModels.InternalModels.NewsInformationModels;

namespace SQLRepositories.NewsRepositories
{
    public class CategoryRepository : EntityBaseRepository<CategoryModel>, ICategoryRepository
    {
        public CategoryRepository(IDBEconomicInformationServiceContext context) : base(context)
        {}

        public void Create(CategoryModel item)
        {
            Context.Categories.Add(item);
            Context.SaveChanges();
        }

        public CategoryModel GetById(Guid id)
        {
            var result = from cat in Context.Categories where cat.Id.Equals(id) select cat;
            return result.FirstOrDefault();
        }

        public void Delete(Guid id)
        {
            var result = from cat in Context.Categories where cat.Id.Equals(id) select cat;
            var item = result.FirstOrDefault();
            if(item == null)
                throw new EntityNotFoundException("Category");
            Context.Categories.Remove(item);
            Context.SaveChanges();
        }

        public void Delete(CategoryModel item)
        {
            Context.Categories.Remove(item);
            Context.SaveChanges();
        }

        public IEnumerable<CategoryModel> GetAll()
        {
            return Context.Categories;
        }
    }
}
